package reporting;

/* ------------------
 * BarChartDemo5.java
 * ------------------
 * (C) Copyright 2002-2006, by Object Refinery Limited.
 * 
 * Source:
 * www.jfree.org/jfreechart/BarChartDemo5.java
 * 
 */
import java.awt.Color;
import java.awt.Font;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * A simple demonstration application showing how to create a horizontal bar 
 * chart.
 */
@SuppressWarnings("serial")
public class BarGraph extends JPanel {
	
	private static ReportHandler handler;
	private String title;
	private String x_Axis;
	private String y_Axis;
	
	private JFreeChart chart;

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    public BarGraph(String cTitle, String xAxis, String yAxis) {
    	title = cTitle;
    	x_Axis = xAxis;
    	y_Axis = yAxis;
    }
    

    /**
     * Creates a sample dataset.
     * 
     * data[0][0] = hotel name
     * data[0][1] = total cost
     * 
     * @return A sample dataset.
     */
    public static CategoryDataset createDatasetForStatistics(String sDate, String eDate) {
        handler = new ReportHandler();
        
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        String seriesKey = "Hotels";

        String[][] data = handler.getProfit(sDate, eDate);

        for(int i=0;i < data.length;i++) {
        	dataset.addValue(Double.parseDouble(data[i][1]), seriesKey, data[i][0].toString());
        }

        return dataset;
    }
    
    /**
     * Creates a sample dataset.
     * 
     * data[0][0] = hotel name
     * data[0][1] = total cost
     * 
     * @return A sample dataset.
     */
    public static CategoryDataset createDatasetForOccupancy(String sDate, String eDate) {
        handler = new ReportHandler();
        
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        String seriesKey = "Hotels";

        String[][] data = handler.getOccupancyRate(sDate, eDate);

        for(int i=0;i < data.length;i++) {
        	dataset.addValue(Double.parseDouble(data[i][1]) / Double.parseDouble(data[i][2]) * 100, seriesKey, data[i][0].toString());
        }

        return dataset;
    }
    
    /**
     * Creates a Statistics chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return A chart.
     */
    public JFreeChart createActualChart(final CategoryDataset dataset) {
        // create the chart...
        JFreeChart chart = ChartFactory.createBarChart(
            title,  // chart title
            x_Axis,                  // domain axis label
            y_Axis,              // range axis label
            dataset,                     // data
            PlotOrientation.HORIZONTAL,  // orientation
            true,                        // include legend
            true,
            false
        );
        chart.addSubtitle(new TextTitle(
                "Source: http://www.homeoffice.gov.uk/rds/pdfs2/r188.pdf", 
                new Font("Dialog", Font.ITALIC, 10)));
        chart.setBackgroundPaint(Color.white);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        CategoryAxis categoryAxis = plot.getDomainAxis();
        categoryAxis.setCategoryMargin(0.0);
        categoryAxis.setUpperMargin(0.02);
        categoryAxis.setLowerMargin(0.02);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setUpperMargin(0.10);

        return chart;
    }
    
    
    /**
     * Used to create the chart
     */
    public void createChart(CategoryDataset data) {
    	this.removeAll();
    	chart = createActualChart(data);
    	JPanel chartPanel = new ChartPanel(chart);
    	this.add(chartPanel);
    }


}
